#include "FileSenderWidget.h"
#include <QtGui/QApplication>
#include <SendRecvManager.h>
#include <string>
#include <Poco/NumberParser.h>
#include <QMessageBox>
#include <Poco/File.h>
#include <Poco/URI.h>
#include <Poco/Path.h>
int main(int argc, char *argv[])
{


	QApplication a(argc, argv);
	if (argc < 4)
        return 0;
	QString aDirPath = QCoreApplication::applicationDirPath () ;
        //QMessageBox::information(NULL, aDirPath, aDirPath);
        std::string aDir = aDirPath.toStdString() + "/tmp/" + std::string(argv[3]);
        Poco::File aFile(aDir);
        aFile.createDirectories();

        std::string aFilePath = std::string(argv[2]);
        std::string aNewPath;
        Poco::URI::decode(aFilePath , aNewPath);
        aNewPath = Poco::Path::transcode(aNewPath);
        //QMessageBox::information(NULL , "aa" , argv[2]);

	FileSenderWidget w;
        FileSenderWidget *pW = &w;
        QString aTitle;
        if (qApp->arguments().at(1) == "0" )
            aTitle = w.property("SendingText").toString();
        else
            aTitle = w.property("RecevingText").toString();

        aTitle+=  QString::fromStdString(aNewPath);/*qApp->arguments().at(2)*/;

        w.setWindowTitle(aTitle);

	if(argc == 5)
		w.hide();
	else
		w.show();


        SendRecvManager aManager(atoi(argv[1]), aNewPath , Poco::NumberParser::parse64( argv[3]  ) , aDir);
		if(argc == 5)
			aManager.SetID(std::string(argv[3])); 
        SendRecvManager* aThread = &aManager;

        QObject::connect(aThread , SIGNAL(finished()) , &a , SLOT(quit() ) );
        QObject::connect(aThread , SIGNAL(TotalPiecesCount(int)) , pW , SIGNAL(numPieces(int)) );
        QObject::connect(aThread , SIGNAL(PieceSent(int)) , pW , SIGNAL(currentPieces(int)) );
        QObject::connect(aThread , SIGNAL(PieceReceived(int)) , pW , SIGNAL(currentPieces(int)) );
        QObject::connect(pW , SIGNAL(CancelAccepted()) , &a , SLOT(quit() ) );

        aManager.start();

	return a.exec();
}
